package leetcode; /**
 * description:
 * author:张腾
 * date:2021-07-15
 */

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 树的中序遍历（非递归方式）
 */
public class leetcode94 {
    public List<Integer> inorderTraversal(TreeNode root) {
        ArrayList<Integer> res = new ArrayList<>();
        LinkedList<TreeNode> stack = new LinkedList<>();

        while (root!=null || !stack.isEmpty()){
            while (root!=null){
                stack.push(root);
                root = root.left;
            }
            root = stack.pop();
            res.add(root.val);
            root = root.right;

        }
        return res;
    }
}
